package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.utils.AlertUtils;
import com.example.demo.utils.StageUtils;
import com.example.demo.yjcx.BasePriceTypeEnum;
import com.example.demo.yjcx.dao.BasePriceDO;
import com.example.demo.zb.ChannelEnum;
import com.example.demo.zb.ChannelTextField;
import com.example.demo.zb.dao.ChannelDO;
import com.example.demo.zb.dao.ChannelDOMapper;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.example.demo.utils.AlertUtils.showAlert;

/**
 * @author linjingze
 * @date 2024/1/19 5:49 PM
 */
@Controller
public class ZbController {

    @Autowired
    private ChannelDOMapper channelMapper;


    List<ChannelTextField> channelTextFieldList = new ArrayList<>();
    public void showConfigView() {
        Stage dialogStage = new Stage();
        List<ChannelDO> channelList = channelMapper.selectList(new QueryWrapper<>());
        Map<String, List<ChannelDO>> channelMap = CollectionUtils.emptyIfNull(channelList)
                .stream().collect(Collectors.groupingBy(ChannelDO::getType));
        // 创建垂直布局用于整个页面
        VBox mainLayout = new VBox(10); // 10是元素之间的间隔
        Button saveButton = new Button("保存");
        // 设置按钮点击事件（按需实现逻辑）
        saveButton.setOnAction(event -> {
            for (ChannelTextField channelTextField : channelTextFieldList) {
                Integer id = channelTextField.getId();
                ChannelDO channelDO = new ChannelDO();
                channelDO.setName(channelTextField.getTextField().getText());
                channelDO.setType(channelTextField.getType());
                if (id == null) {
                    channelMapper.insert(channelDO);
                }else{
                    channelDO.setId(id);
                    channelMapper.updateById(channelDO);
                }
            }
            dialogStage.close(); // 关闭对话框
            AlertUtils.showAlert("保存成功");
        });
        ObservableList<Node> children = mainLayout.getChildren();
        for (ChannelEnum value : ChannelEnum.values()) {
            if (value == ChannelEnum.DEFAULT) {
                continue;
            }
            children.add(getvBox(channelMap, value));
        }
        children.add(saveButton);
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(mainLayout);

        scrollPane.setFitToHeight(true); // 让VBox宽度适应ScrollP
        // 设置场景和舞台
        Scene scene = new Scene(scrollPane);
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(StageUtils.stage);
        dialogStage.setTitle("渠道配置");
        dialogStage.setScene(scene);
        dialogStage.setWidth(400);
        dialogStage.setHeight(600);
        dialogStage.show();


    }

    private VBox getvBox(Map<String, List<ChannelDO>> channelMap, ChannelEnum channelEnum) {
        channelTextFieldList.clear();
        String name = channelEnum.name();
        VBox chiBox = new VBox(5);
        Label chiLabel = new Label(name + ":");
        HBox titleAndButtonContainer = new HBox(5); // 创建包含标题和按钮的水平布局

        // 创建添加按钮
        Button addButton = new Button("添加");
        titleAndButtonContainer.getChildren().addAll(chiLabel, addButton);
        chiBox.getChildren().addAll(titleAndButtonContainer);
        addButton.setOnAction(event -> {
            // 在这个分区添加一个新的输入框
            TextField e = new TextField();
            ChannelTextField channelTextField = new ChannelTextField();
            channelTextField.setTextField(e);
            channelTextField.setType(name);
            chiBox.getChildren().add(e);
            channelTextFieldList.add(channelTextField);
        });
        List<ChannelDO> chiList = channelMap.getOrDefault(name, Arrays.asList(new ChannelDO(), new ChannelDO()));
        for (ChannelDO channelDO : chiList) {
            TextField e = new TextField(channelDO.getName());
            ChannelTextField channelTextField = new ChannelTextField();
            channelTextField.setId(channelDO.getId());
            channelTextField.setTextField(e);
            channelTextField.setType(name);
            channelTextFieldList.add(channelTextField);
            chiBox.getChildren().add(e);
        }
        return chiBox;
    }
}
